import edu.udo.cs.wvtool.generic.output.WVTOutputFilter;
import edu.udo.cs.wvtool.main.WVTWordVector;
import edu.udo.cs.wvtool.util.WVToolException;
import net.sf.javaml.core.Dataset;
import net.sf.javaml.core.DefaultDataset;
import net.sf.javaml.core.Instance;
import net.sf.javaml.core.SparseInstance;
import net.sf.javaml.distance.PearsonCorrelationCoefficient;
import net.sf.javaml.featureselection.subset.GreedyForwardSelection;
import net.sf.javaml.tools.data.FileHandler;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: tobias
 * Date: 5/5/11
 * Time: 4:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class DataSet extends DefaultDataset implements WVTOutputFilter {

    public DataSet() {
        super();
    }

    public DataSet(Dataset dataset) {
        for(Instance i : dataset) {
            this.add(i);
        }
    }

    public void write(WVTWordVector wv) throws WVToolException {
        write(new WVector(wv));
    }
    public void write(WVector wv) throws WVToolException {
        double[] values = wv.getValues();
		String label = wv.getLabel() + "";
		Instance instance = new SparseInstance();
		instance.setClassValue(label);

		for(int i = 0; i < values.length; i++) {
			if(values[i] > 0.0) {
				instance.put(i, values[i]);
			}
		}
        this.add(instance);
    }
    public void setFeatureSetSize(int n) {
        Set<Integer> rm_attr = new HashSet<Integer>();
        GreedyForwardSelection ga =
                new GreedyForwardSelection(n,
                        new PearsonCorrelationCoefficient());
        ga.build(this);
        System.out.println(noAttributes());
        for(int i = 0; i < 285; i++) {
            //String si = i + "";
			if(!ga.selectedAttributes().contains(i)) {
                System.out.println("hej");
				rm_attr.add(i);
            }
		}
		for(Instance inst : this) {
			inst.removeAttributes(rm_attr);
		}
    }
    public void store(File file) throws IOException {
        FileHandler.exportDataset(this, file);
    }


}
